package cn.jinhaze;

import cn.jinhaze.constant.SexConstant;
import cn.jinhaze.entity.Clazz;
import cn.jinhaze.entity.Student;
import cn.jinhaze.enums.Course;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author LiZhiHan
 * @date 2025/2/20
 */
public class Main {

	private static final List<Course> manCourse = Arrays.asList(
			Course.CHINESE, Course.ENGLISH, Course.MATH, Course.SANDA, Course.PANYAN);

	private static final List<Course> womenCourse = Arrays.asList(
			Course.CHINESE, Course.ENGLISH, Course.MATH, Course.DANCE, Course.TAIJI);

	public static void main(String[] args) {
		Clazz clazz = new Clazz(999L, "软工23-4班", 10);
		clazz.addStudent(new Student(1L, "学生1", SexConstant.MAN, 999L, buildCourse(SexConstant.MAN)));
		clazz.addStudent(new Student(2L, "学生2", SexConstant.MAN, 999L, buildCourse(SexConstant.MAN)));
		clazz.addStudent(new Student(3L, "学生3", SexConstant.MAN, 999L, buildCourse(SexConstant.MAN)));
		clazz.addStudent(new Student(4L, "学生4", SexConstant.MAN, 999L, buildCourse(SexConstant.MAN)));
		clazz.addStudent(new Student(5L, "学生5", SexConstant.MAN, 999L, buildCourse(SexConstant.MAN)));
		clazz.addStudent(new Student(6L, "学生6", SexConstant.WOMEN, 999L, buildCourse(SexConstant.WOMEN)));
		clazz.addStudent(new Student(7L, "学生7", SexConstant.WOMEN, 999L, buildCourse(SexConstant.WOMEN)));
		clazz.addStudent(new Student(8L, "学生8", SexConstant.WOMEN, 999L, buildCourse(SexConstant.WOMEN)));
		clazz.addStudent(new Student(9L, "学生9", SexConstant.WOMEN, 999L, buildCourse(SexConstant.WOMEN)));
		clazz.addStudent(new Student(10L, "学生10", SexConstant.WOMEN, 999L, buildCourse(SexConstant.WOMEN)));

		// 求选择数学的
		List<Student> students = clazz.getStudents();
		List<Student> mathStudent = students.stream().filter(student -> {
			Set<Course> courses = student.getCourses();
			boolean res = false;
			for (Course course : courses) {
				if ("数学".equals(course.getName())) {
					res = true;
					break;
				}
			}
			return res;
		}).collect(Collectors.toList());
		System.out.println("===选数学的学生===");
		for (Student student : mathStudent) {
			System.out.println(student.getName());
		}
		// 求英语课的男女比例
		// 过滤出选择英语的
		List<Student> englishStudent = students.stream().filter(student -> {
			Set<Course> courses = student.getCourses();
			boolean res = false;
			for (Course course : courses) {
				if ("英语".equals(course.getName())) {
					res = true;
					break;
				}
			}
			return res;
		}).collect(Collectors.toList());
		// 求男生女生数
		int englishManNum = 0;
		int englishWomanNum = 0;
		for (Student student : englishStudent) {
			if (SexConstant.WOMEN.equals(student.getSex())) {
				englishWomanNum++;
			} else {
				englishManNum++;
			}
		}
		System.out.println("===选英语课的男女比例===");
		System.out.println("男生 : 女生 = " + englishManNum + " : " + englishWomanNum);

		List<Student> womenStudent = students.stream()
											 .filter(student -> SexConstant.WOMEN.equals(student.getSex())).collect(Collectors.toList());

		List<Student> manStudent = students.stream()
										   .filter(student -> SexConstant.MAN.equals(student.getSex())).collect(Collectors.toList());

		// 男女学生列表
		System.out.println("===男女学生列表===");
		System.out.println("男生列表:" + manStudent);
		System.out.println("女生列表" + womenStudent);

		// 输入课程名称
		System.out.println("===输入课程名称===");
		String key = new Scanner(System.in).next();
		List<Student> courseStudent = getCourseStudent(key, students);
		System.out.print("选择" + key + "的学生: ");
		for (Student student : courseStudent) {
			System.out.print(student.getName() + " ");
		}


	}

	private static List<Student> getCourseStudent(String key, List<Student> students) {
		return students.stream().filter(student -> {
			Set<Course> courses = student.getCourses();
			boolean res = false;
			for (Course course : courses) {
				if (key.equals(course.getName())) {
					res = true;
					break;
				}
			}
			return res;
		}).collect(Collectors.toList());
	}

	public static Set<Course> buildCourse(Integer sex) {
		Set<Course> resCourse = new HashSet<>();
		Random random = new Random();
		if (SexConstant.WOMEN.equals(sex)) {
			// 性别为女生
			for (int i = 0; i < 3; i++) {
				resCourse.add(womenCourse.get(Math.abs(random.nextInt()) % womenCourse.size()));
			}

		} else {
			// 性别为男生
			for (int i = 0; i < 3; i++) {
				resCourse.add(manCourse.get(Math.abs(random.nextInt()) % manCourse.size()));
			}
		}
		return resCourse;
	}


}
